package cn.yangliu.jpa.page;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

public class Page extends PageRequest {

	public static int PAGE_SIZE= 10;
	private static final long serialVersionUID = 1L;
	private int index;
	private int size=PAGE_SIZE;
	private Sort sort;

	public int getIndex() {
		return index;
	}
	public void setIndex(int index) {
		this.index = index;
	}
	public int getPage() {
		return index;
	}
	public void setPage(int index) {
		this.index = index;
	}
	public int getSize() {
		return size;
	}
	public void setSize(int size) {
		this.size = size;
	}
	public void setSort(Sort sort) {
		this.sort = sort;
	}
	public Page(){
		super(0, PAGE_SIZE);
	}
	public Page(int index, int size) {
		super(index, size, null);
	}


	public Page(int index, int size, Direction direction, String... properties) {
		super(index, size, new Sort(direction, properties));
	}

	public int getPageSize() {

		return size;
	}

	public int getPageNumber() {
		return index;
	}


	public int getOffset() {
		return index * size;
	}

	
	public Sort getSort() {
		return sort;
	}


	public boolean hasPrevious() {
		return index > 0;
	}

	
	public Pageable next() {
		return new PageRequest(index + 1, size, sort);
	}


	public Pageable previousOrFirst() {
		return hasPrevious() ? new PageRequest(index - 1, size, sort) : this;
	}

	public Pageable first() {
		return new PageRequest(0, size, sort);
	}

	@Override
	public String toString() {
		return String.format("Page request [number: %d, size %d, sort: %s]", index, size,
				sort == null ? null : sort.toString());
	}

}
